﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;//ControlCommands;
using ESRI.ArcGIS.SystemUI;

namespace GISCommon
{
    public partial class RCITS_MapCtrl : UserControl
    {
        public GISCommon.Business.MapToolsHelper m_MapTools = null;//
        public RCITS_MapCtrl()
        {
           //ESRI.ArcGIS.Controls.AxLicenseControl axLicenseControl1 = new AxLicenseControl();
           // ((System.ComponentModel.ISupportInitialize)(axLicenseControl1)).BeginInit();

           // axLicenseControl1.Enabled = true;
           // axLicenseControl1.Location = new System.Drawing.Point(50, 3);
           // axLicenseControl1.Name = "axLicenseControl1";
           // //this.axLicenseControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axLicenseControl1.OcxState")));
           // axLicenseControl1.Size = new System.Drawing.Size(32, 32);
           // axLicenseControl1.TabIndex = 1;
           // this.Controls.Add(axLicenseControl1);

            InitializeComponent();

            //((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).EndInit();
        }

        bool _hasContextMenu = true;
        public bool HasContextMenu
        {
            get { return _hasContextMenu; }
            set { _hasContextMenu = value; }
        }

        public void init()
        {
            try
            {
                m_MapCtrl = new ESRI.ArcGIS.Controls.AxMapControl();
                m_MapCtrl.Width = this.Width;
                m_MapCtrl.Height = this.Height;
                m_MapCtrl.Dock = DockStyle.Fill;
                this.Controls.Add(m_MapCtrl);
                m_MapCtrl.BorderStyle = esriControlsBorderStyle.esriBorder;
                m_MapCtrl.ShowScrollbars = false;

                InitMenuEvent();
                m_MapTools = new GISCommon.Business.MapToolsHelper(m_MapCtrl);
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("RCITS_MapCtrl.init()出错", ex);
            }
        }

        #region Right Menu Function
        private void InitMenuEvent()
        {
            this.mu_MapZoomIn.Click += new EventHandler(mu_ZoomIn_Click);
            this.mu_MapZoomOut.Click += new EventHandler(mu_MapZoomOut_Click);
            this.mu_MapPan.Click += new EventHandler(mu_MapPan_Click);
            this.mu_ShowAllMap.Click += new EventHandler(mu_ShowAllMap_Click);
            this.mu_MapSelect.Click += new EventHandler(mu_MapSelect_Click);

            //this.m_MapCtrl.OnMouseDown += new IMapControlEvents2_OnMouseDownEventHandler(m_MapCtrl_MouseDown);
            this.m_MapCtrl.OnMouseDown += new IMapControlEvents2_Ax_OnMouseDownEventHandler(m_MapCtrl_OnMouseDown);
        }

        void mu_MapSelect_Click(object sender, EventArgs e)
        {
            m_MapTools.Cancel();
        }

        void mu_ShowAllMap_Click(object sender, EventArgs e)
        {
            m_MapTools.FullExtent();
        }

        void m_MapCtrl_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            switch (e.button)
            {
                case 1:
                    //ddd(e);
                    break;
                case 2:
                    if (HasContextMenu)
                    {
                        Point p1 = new Point(e.x, e.y);
                        mu_MapRight.Show(m_MapCtrl, p1);
                    }
                    break;
            }
        }


        void mu_MapPan_Click(object sender, EventArgs e)
        {
            m_MapTools.Pan();
        }

        void mu_MapZoomOut_Click(object sender, EventArgs e)
        {
            m_MapTools.ZoomOut();
        }

        void mu_ZoomIn_Click(object sender, EventArgs e)
        {
            m_MapTools.ZoomIn();
        }

        #endregion

        private ESRI.ArcGIS.Controls.AxMapControl m_MapCtrl = null;
        public ESRI.ArcGIS.Controls.AxMapControl RCITSMapCtrl
        {
            set { m_MapCtrl = value; }
            get { return m_MapCtrl; }
        }


    }
}